package cn.mln521.test;

import cn.mln521.annotation.demo1.UserService;
import cn.mln521.annotation.demo2.Bean1;
import cn.mln521.annotation.demo2.Bean2;
import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SpringDemo2 {

    /**当bean被载入到容器的时候调用
     *  注解方式如下:
     *    @PostConstruct
     *      初始化
     * 当bean从容器中删除的时候调用 teardown(scope= singleton有效)
     *  注解方式如下:
     *      @PreDestroy
     *         销毁
     */
    @Test
    public void demo1() {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("application.xml");
        Bean1 bean1 = (Bean1) context.getBean("bean1");
        bean1.say();

        context.close();
    }

    /**
     * @Scope注解用于指定Bean的作用范围
     * 默认作用范围都是 singleton
     *                prototype
     */
    @Test
    public void demo2() {
        ApplicationContext context = new ClassPathXmlApplicationContext("application.xml");
        Bean2 bean1 = (Bean2) context.getBean("bean2");
        Bean2 bean2 = (Bean2) context.getBean("bean2");

        System.out.println(bean1 == bean2);
    }

}
